import com.alibaba.alimonitor.jmonitor.utils.StringUtils
import com.alibaba.moco.RedisPoolInstance
import com.alibaba.moco.util.BaseTypeUtil
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import redis.clients.jedis.Jedis
import script.GlobalValues
import script.moco.dto.DriverArriveRequest
import script.moco.dto.DriverArriveResponse
import script.moco.dto.OrderInfo
import script.moco.dto.OrderPriceDetail
import script.v1.didiCallback.NoticeCallback
import script.v1.didiCallback.NotifyRequest
//模拟司机发起收款
def excute(data) {

    Jedis redis = RedisPoolInstance.getRedisClient();
    def params = data.getParameterMap();
    def request = BaseTypeUtil.mapToBean(params, DriverArriveRequest.class) as DriverArriveRequest;//请求数据转换为对象

    def response = new DriverArriveResponse();    //定义响应对象

    def jsonStr = redis.get(GlobalValues.KEY_ORDER + request.getOrderId());//从 Redis中获取 json 订单字符串

    if (jsonStr == null) {    //验证订单是否存在
        response.setSuccess("false");
        response.setErrorMsg("订单id不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }

    def slurper = new JsonSlurper();
    def detailMap = slurper.parseText(jsonStr);

    if (!detailMap.containsKey("order")) {
        response.setSuccess("false");
        response.setErrorMsg("订单不存在 !");
        response.setErrorCode(39001);
        response.setErrorLevel(1);

        return response;
    }

    def jsonMap = detailMap.get("order");
    def taxiObj = BaseTypeUtil.jsonToBean(jsonMap, OrderInfo.class) as OrderInfo;//将json对象转换为OrderInfo对象

    //司机发起收款完善数据
    taxiObj.setDlng(request.getDriverLongitude());
    taxiObj.setDlat(request.getDriverLatitude());
    //taxiObj.setDriverGeotype(request.getDriverGeotype());
    taxiObj.setStatus(600);
    taxiObj.setSub_status(7000);
    //添加费用详情
    if (detailMap.containsKey("price")) {
        OrderPriceDetail det = new OrderPriceDetail();
        System.out.println("设置费用详情normal_fee")
        det.setAmount(detailMap.price.total_price)
        det.setName("正常行驶费用\"或\"超出套餐部分行驶距离费")
        det.setType("normal_fee")
        System.out.println(detailMap.price.detail)

        detailMap.price.detail.add(det);
        //系统自动检测合理需添加高速费详情
        OrderPriceDetail deta = new OrderPriceDetail();
        System.out.println("设置高速费highway_fee")
        deta.setAmount("10")
        deta.setName("高速费")
        deta.setType("highway_fee")
        System.out.println(detailMap.price.detail)

        detailMap.price.detail.add(deta);
    }
    detailMap.put("order", taxiObj);

    redis.set(GlobalValues.KEY_ORDER + taxiObj.getId(), JsonOutput.toJson(detailMap).toString())    //直接覆盖原有数据，进行修改

    //通知回调
    NoticeCallback noticeCallbackTool = new NoticeCallback();
    NotifyRequest notifyRequest = new NotifyRequest();
    notifyRequest.setOrderId(taxiObj.getId());
    notifyRequest.setNotifyType(3);
    notifyRequest.setNotifyDesc("订单支付确认通知[订单正常结束待支付（确认支付或费用投诉）]");
    String url = noticeCallbackTool.getCallbackUrlFromRequest(data)
    if (StringUtils.isNotBlank(url)) {
        response.setSuccess("true");
        response.setErrorMsg(null);
        response.setErrorCode(0);
        response.setErrorLevel(0);
        response.setContent(noticeCallbackTool.handNotify(url, notifyRequest));
    } else {
        response.setSuccess("false");
        response.setErrorMsg("session 中不存在callback地址");
        response.setErrorCode(-1);
        response.setErrorLevel(0);
    }
    return response;
}